 package com.example.mybaiduweishi.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.mybaiduweishi.MyApplication;
import com.example.mybaiduweishi.R;
import com.example.mybaiduweishi.activity.InsertContactsActivity;
import com.example.mybaiduweishi.fragments.base.BaseFragmentbase;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class secondFragment extends BaseFragmentbase {
    private Map<Integer, String> map = new HashMap<>();
    String[] infos;


    @BindView(R.id.edit_insert_sos)
    EditText edit;
    @BindView(R.id.check_one_sos)
    CheckBox check;

    Unbinder unbinder;

    @BindView(R.id.img_insert_sos)
    ImageView imgInsertSos;
    @BindView(R.id.btn_insert_sos)
    Button btnInsertSos;
    @BindView(R.id.btn_yes_second)
    Button btnYesSecond;
    @BindView(R.id.second_recyclerview)
    RecyclerView recyclerview;


    @Override
    public int getXML() {
        return R.layout.second_fragment;
    }

    @Override
    public void initView() {
      edit.addTextChangedListener(new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {

          }

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
            imgInsertSos.setVisibility(View.GONE);
              btnInsertSos.setVisibility(View.VISIBLE);
          }

          @Override
          public void afterTextChanged(Editable s) {

          }
      });
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.img_insert_sos, R.id.btn_insert_sos, R.id.btn_yes_second})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_insert_sos:
                //添加联系人
                Intent intent = new Intent(getActivity(), InsertContactsActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_insert_sos:
                //添加联系人之后的确定
                saveEdit();

                break;
            case R.id.edit_insert_sos:

                break;
            case R.id.btn_yes_second:
                //完成这个页面的按钮
                break;

        }
    }

    private void saveEdit() {
        String tel = edit.getText().toString().trim();
        /**
         * 中国电信号码格式验证 手机段： 133,153,180,181,189,177,1700,173
         * **/
        final String CHINA_TELECOM_PATTERN = "(^1(33|53|7[37]|8[019])\\d{8}$)|(^1700\\d{7}$)";

        /**
         * 中国联通号码格式验证 手机段：130,131,132,155,156,185,186,145,176,1707,1708,1709
         * **/
        final String CHINA_UNICOM_PATTERN = "(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^170[7-9]\\d{7}$)";

        /**
         * 中国移动号码格式验证
         * 手机段：134,135,136,137,138,139,150,151,152,157,158,159,182,183,184
         * ,187,188,147,178,1705
         *
         **/
        final String CHINA_MOBILE_PATTERN = "(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";

        /**
         * 仅手机号格式校验
         */
        final String PHONE_PATTERN=new StringBuilder(300).append(CHINA_MOBILE_PATTERN)
                .append("|")
                .append(CHINA_TELECOM_PATTERN)
                .append("|")
                .append(CHINA_UNICOM_PATTERN)
                .toString();
        Pattern pattern = Pattern.compile(PHONE_PATTERN);
        Matcher matcher = pattern.matcher(edit.getText());

        if (matcher.find()){
            edit.setText("");
            imgInsertSos.setVisibility(View.VISIBLE);
            btnInsertSos.setVisibility(View.GONE);
        }else {
            Toast.makeText(MyApplication.getContext(), "输入的手机号码不正确", Toast.LENGTH_SHORT).show();
        }


    }


}
